import Item from "antd/es/list/Item";
import FetchHelper from "../helper/fetch-helper";
import { isIntersects, wktToGeom } from "../helper/mapHelper";
import _ from 'lodash';

export default class CszhApi {
    static async listByPolygon(polygon) {
        // const resp = await FetchHelper.postJson(`/api/yuqing_datafindby`, params)
        const resp = await FetchHelper.getJson(`/admin-api/ybyj/jzzy/list`)
        const datas = resp.rows?.filter(item => {
            if (!item.geom) {
                return false;
            }
            const geom = wktToGeom(item.geom);
            return isIntersects(geom, polygon)
        })
        const groupData = _.groupBy(datas || [], 'cszhlx');
        return {
            data: _.keys(groupData).map(key => {
                return {
                    name: key,
                    count: groupData[key].length,
                    list: groupData[key]
                }
            })
        };
    }
    static async list(params) {
        try {
            const resp = await FetchHelper.getJson(`/admin-api/ybyj/cszhsjb/list`, {
                ...params
            })
            resp.rows = resp?.rows?.map(item => {

                if (item?.xq) {
                    try {
                        const json = JSON.parse(item?.xq);
                        return {
                            ...json,
                            ...item,
                        }
                    } catch (e) {

                    }
                }
                return item;
            })
            return resp;
        } catch (err) {
            return { rows: [] }
        }
    }
     //根据空间返回次生灾害类型与数量
    static async listByPolygon2(wkt) {
        const resp = await FetchHelper.getJson(`/admin-api/ybyj/cszhsjb/countByLxAndWkt`, {
            wkt: wkt
        })
        return resp;
    }
    //根据空间和次生灾害类型返回id
    static async cszhWkt(wkt,cszhlx) {
        const resp = await FetchHelper.getJson(`/admin-api/ybyj/cszhsjb/listCszhByXlAndWkt`, {
            wkt: wkt,
            cszhlx:cszhlx,
        })
        return resp;
    }
    //根据id查询次生灾害数据
    static async listId(id) {
        const resp = await FetchHelper.getJson(`/admin-api/ybyj/cszhsjb/list`, {
            id:id,
        })
        return resp;
    }
}
